package com.schoolcloud.api.config;

import cn.hutool.core.util.StrUtil;
import com.schoolcloud.common.exception.ServiceException;
import com.schoolcloud.common.util.UserContext;
import feign.InvocationContext;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import feign.ResponseInterceptor;
import feign.codec.Decoder;
import feign.codec.ErrorDecoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


public class DefaultFeignConfig {
    /**
     * 远程调用时携带用户信息（feign请求）
     *
     * @return {@link RequestInterceptor}
     */
    @Bean
    public RequestInterceptor userInfoInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {
                try{
                    String user = UserContext.getUser();
                    String userType = UserContext.getUserType();
                    boolean isAdmin = UserContext.getIsAdmin();
                    template.header("user-id", user);
                    template.header("user-type", userType);
                    template.header("is-admin",String.valueOf(isAdmin));
                }
                catch (Exception ignored){}
            }
        };
    }
    @Bean
    public Decoder feignDecoder() {
        return new FeignResponseInterceptor();
    }
}
